package com.neusoft.acquistion.sensor.consumer.service.impl;

import com.neusoft.acquistion.sensor.consumer.entity.Sensordata;
import com.neusoft.acquistion.sensor.consumer.mapper.SensordataMapper;
import com.neusoft.acquistion.sensor.consumer.service.IPatientService;
import com.neusoft.acquistion.sensor.consumer.service.ISensordataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 收集来自传感器的数据 服务实现类
 * </p>
 *
 * @author 段静迪
 * @since 2024-04-26 14:27:10
 */
@Slf4j
@Service
public class SensordataServiceImpl extends ServiceImpl<SensordataMapper, Sensordata> implements ISensordataService {

    @Autowired
    private IPatientService patientService;

    @Override
    public void wiriteMonitorData(Map<String, Object> monitorMap) {
        final  String  serialNumber = (String) monitorMap.get("serialNumber");
        monitorMap.remove("serialNumber");
        monitorMap.keySet().forEach(key -> {
            Sensordata sensordata = new Sensordata();
            sensordata.setSensornumber( Integer.parseInt(serialNumber) );
            sensordata.setDt(LocalDateTime.now());
            sensordata.setData(monitorMap.get(key).toString());
            sensordata.setItem(key);
            sensordata.setPersonid(  patientService.findPatientIdById(serialNumber)  );
            sensordata.setMedicalcode(Long.parseLong(serialNumber));
            sensordata.setSensornumber(1);
            log.error(  sensordata.toCSV()  );
        });
    }
}
